//package com.skynet.supervision.es;
//
//import cn.hutool.json.JSONUtil;
//import com.skynet.supervision.es.domain.FileObj;
//import dm.jdbc.util.StringUtil;
//import lombok.SneakyThrows;
//import org.elasticsearch.action.get.GetRequest;
//import org.elasticsearch.action.get.GetResponse;
//import org.elasticsearch.action.index.IndexRequest;
//import org.elasticsearch.action.index.IndexResponse;
//import org.elasticsearch.action.search.SearchRequest;
//import org.elasticsearch.action.search.SearchResponse;
//import org.elasticsearch.client.RequestOptions;
//import org.elasticsearch.client.RestHighLevelClient;
//import org.elasticsearch.common.xcontent.XContentType;
//import org.elasticsearch.index.query.QueryBuilders;
//import org.elasticsearch.search.builder.SearchSourceBuilder;
//import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
//import org.springframework.stereotype.Service;
//
//import java.util.Map;
//
//@Service
//public class ElasticOperation {
//
//    private final RestHighLevelClient client;
//
//    //索引
//    private static String Index = "docwrite";
//
//    public ElasticOperation(RestHighLevelClient client) {
//        this.client = client;
//    }
//
//
//
//    @SneakyThrows
//    public void upload(FileObj file) {
//        IndexRequest indexRequest = new IndexRequest(Index);
//        //indexRequest.id(file.getId());
//
//        //上传同时，使用attachment pipline进行提取文件
//        indexRequest.source(JSONUtil.toJsonStr(file), XContentType.JSON);
//        indexRequest.setPipeline("attachment");
//        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
//
////        System.out.println(indexResponse);
//    }
//
//    @SneakyThrows
//    public SearchResponse search(LawPageRequest request){
//        SearchRequest searchRequest = new SearchRequest(Index);
//        //默认会search出所有的东西来
//        //SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        //使用lk分词器查询，会把插入的字段分词，然后进行处理
//        SearchSourceBuilder srb = new SearchSourceBuilder();
//        if(StringUtil.isEmpty(request.getKey())){
//           request.setKey("法规");
//        }
//        srb.query(QueryBuilders.matchQuery("attachment.content", request.getKey()).analyzer("ik_smart"));
//        //设置highlighting
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        HighlightBuilder.Field highlightContent = new HighlightBuilder.Field("attachment.content");
//        highlightContent.highlighterType();
//        highlightBuilder.field(highlightContent);
//        highlightBuilder.preTags("<em>");
//        highlightBuilder.postTags("</em>");
//
//        //highlighting会自动返回匹配到的文本，所以就不需要再次返回文本了
//        String[] includeFields = new String[]{"name","createTime","createUser","type"};
//        String[] excludeFields = new String[]{"attachment.content"};
//        srb.fetchSource(includeFields, excludeFields);
//        srb.size(request.getPageSize());
//        srb.from(request.getPage()-1);
//        //把刚才设置的值导入进去
//        srb.highlighter(highlightBuilder);
//        searchRequest.source(srb);
//        SearchResponse res = client.search(searchRequest, RequestOptions.DEFAULT);
//        return res;
//    }
//
//    @SneakyThrows
//    public SearchResponse appSearch(LawPageRequest request){
//        SearchRequest searchRequest = new SearchRequest(Index);
//        //默认会search出所有的东西来
//        //SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//        //使用lk分词器查询，会把插入的字段分词，然后进行处理
//        SearchSourceBuilder srb = new SearchSourceBuilder();
//        if(StringUtil.isEmpty(request.getKey())){
//            request.setKey("法规");
//        }
//        srb.query(QueryBuilders.matchQuery("attachment.content", request.getKey()).analyzer("ik_smart"));
//        //设置highlighting
//        HighlightBuilder highlightBuilder = new HighlightBuilder();
//        HighlightBuilder.Field highlightContent = new HighlightBuilder.Field("attachment.content");
//        highlightContent.highlighterType();
//        highlightBuilder.field(highlightContent);
//        highlightBuilder.preTags("");
//        highlightBuilder.postTags("");
//
//        //highlighting会自动返回匹配到的文本，所以就不需要再次返回文本了
//        String[] includeFields = new String[]{"name","createTime","createUser","type"};
//        String[] excludeFields = new String[]{"attachment.content"};
//        srb.fetchSource(includeFields, excludeFields);
//        srb.size(request.getPageSize());
//        srb.from(request.getPage()-1);
//        //把刚才设置的值导入进去
//        srb.highlighter(highlightBuilder);
//        searchRequest.source(srb);
//        SearchResponse res = client.search(searchRequest, RequestOptions.DEFAULT);
//        return res;
//    }
//
//    @SneakyThrows
//    public Map<String,Object> getDetail(String id) {
//        GetRequest request=new GetRequest(Index);
//        GetResponse getResponse = client.get(request.id(id), RequestOptions.DEFAULT);
//        System.out.println(request.preference());
//        Map<String, Object> source = getResponse.getSource();
////        Object attachment = source.("attachment");
//        Map<String,Object> attachment = (Map<String, Object>) source.get("attachment");
//        attachment.put("fileUrl",source.get("fileUrl"));
//        return attachment;
//    }
//}
